package cn.com.surker.material.service.impl;

import cn.com.surker.dto.main.MainMaterialSupplierRateDTO;
import cn.com.surker.material.dao.MainMaterialSupplierRateDao;
import cn.com.surker.material.entity.MainMaterialSupplierRate;
import cn.com.surker.material.service.MainMaterialSupplierRateService;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.vo.main.MainMaterialSupplierRateVO;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.snaker.engine.SnakerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class MainMaterialSupplierRateServiceImpl extends ServiceImpl<MainMaterialSupplierRateDao, MainMaterialSupplierRate>  implements MainMaterialSupplierRateService {


    @Resource
    private MainMaterialSupplierRateDao dao;


    @Override
    public IPage<MainMaterialSupplierRateVO> pageData(MainMaterialSupplierRateDTO dto) {
        PageHelper.offsetPage(dto.getOffsetSize(), dto.getLimit());
        IPage<MainMaterialSupplierRateVO> supplierPage = new Page<>();
        QueryWrapper<MainMaterialSupplierRate> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(dto.getSearchVal())) {
            queryWrapper.lambda().like(MainMaterialSupplierRate::getSupplier,dto.getSearchVal()).or().like(MainMaterialSupplierRate::getRemark,dto.getSearchVal());
        }
        if (ObjectUtil.isNotEmpty(dto.getOrgId())) {
            queryWrapper.eq("org_id", dto.getOrgId());
        }
        if (ObjectUtil.isNotEmpty(dto.getStatus())) {
            queryWrapper.eq("status", dto.getStatus());
        }
        queryWrapper.lambda().orderByDesc(MainMaterialSupplierRate::getCreateTime,MainMaterialSupplierRate::getUpdateTime);
        IPage<MainMaterialSupplierRate> mainSupplierPage = dao.selectPage(new Page<>(dto.getCurrSize(), dto.getLimit()), queryWrapper);
        if (ObjectUtil.isNotEmpty(mainSupplierPage)) {
            supplierPage.setTotal(mainSupplierPage.getTotal());
            supplierPage.setPages(mainSupplierPage.getPages());
            supplierPage.setCurrent(mainSupplierPage.getCurrent());
            supplierPage.setSize(mainSupplierPage.getSize());
            if (CollUtil.isNotEmpty(mainSupplierPage.getRecords())) {
                PageInfo pageInfo = new PageInfo(mainSupplierPage.getRecords());
                if (ObjectUtil.isNotEmpty(pageInfo.getTotal())){
                    supplierPage.setTotal(pageInfo.getTotal());
                }
                List<MainMaterialSupplierRateVO> listVo = new ArrayList<>();
                for (MainMaterialSupplierRate mainMaterialSupplierRate : mainSupplierPage.getRecords()) {
                    MainMaterialSupplierRateVO mainMaterialSupplierRateVO = BeanUtil.copyProperties(mainMaterialSupplierRate, MainMaterialSupplierRateVO.class);
                    listVo.add(mainMaterialSupplierRateVO);
                }
                supplierPage.setRecords(listVo);
            }
        }
        return supplierPage;
    }


    @Override
    public Boolean update(MainMaterialSupplierRateDTO dto, CacheUser user) {
        MainMaterialSupplierRate dbRate = getById(dto.getId());
        if(ObjectUtil.isEmpty(dbRate)){
            throw new SnakerException("当前数据不存在！");
        }
        BeanUtil.copyProperties(dto,dbRate);
        dbRate.setUpdateTime(LocalDateTime.now());
        dbRate.setUpdator(user.getName());
        return updateById(dbRate);
    }


    @Override
    public Boolean updatestatus(MainMaterialSupplierRateDTO dto, CacheUser user) {
        MainMaterialSupplierRate dbRate = getById(dto.getId());
        if(ObjectUtil.isEmpty(dbRate)){
            throw new SnakerException("当前数据不存在！");
        }
        if(ObjectUtil.isEmpty(dto.getStatus())){
            throw new SnakerException("状态数据不存在！");
        }
        dbRate.setStatus(dto.getStatus());
        dbRate.setUpdateTime(LocalDateTime.now());
        dbRate.setUpdator(user.getName());
        return updateById(dbRate);
    }

    @Override
    public Boolean saveRate(MainMaterialSupplierRateDTO dto, CacheUser user) {
        MainMaterialSupplierRate mainMaterialSupplierRate = BeanUtil.copyProperties(dto, MainMaterialSupplierRate.class);
        mainMaterialSupplierRate.setStatus(0);
        mainMaterialSupplierRate.setCreateTime(LocalDateTime.now());
        mainMaterialSupplierRate.setCreator(user.getName());
        return save(mainMaterialSupplierRate);
    }


}
